package com.example.mymarket.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mymarket.Data_model.LoginData;
import com.example.mymarket.Data_model.ResponseData;
import com.example.mymarket.api.ApiService;
import com.example.mymarket.interfaces.ResponseCallback;
import com.example.mymarket.R;

public class LoginActivity extends AppCompatActivity {

    private EditText onUser;
    private EditText onPwd;
    private Button oLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 绑定视图控件
        onUser = findViewById(R.id.login_username_edit);
        onPwd = findViewById(R.id.login_password_edit);
        oLogin = findViewById(R.id.login_login_btn);

        // 设置登录按钮点击事件
        oLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = onUser.getText().toString().trim();
                String password = onPwd.getText().toString().trim();

                // 输入验证
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }



                // 调用API进行登录
                ApiService.login(username, password, new ResponseCallback<ResponseData<LoginData>>() {
                    @Override
                    public void onSuccess(ResponseData<LoginData> response) {
                        // 恢复按钮状态
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                oLogin.setEnabled(true);
                                oLogin.setText("登录");
                            }
                        });

                        // 处理登录响应
                        if (response != null && response.getError() == 0) {
                            LoginData loginData = response.getData();
                            if (loginData != null) {
                                // 保存用户信息到本地
                                saveUserInfo(loginData);
                                Log.e("TAG", "response.getError: " + response.getErrmsg() );
                                // 显示登录成功提示
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                                // 跳转到主界面
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);

                            }
                        } else {
                            // 显示错误信息
                            String errorMsg = response != null && !TextUtils.isEmpty(response.getErrmsg())
                                    ? response.getErrmsg()
                                    : "登录失败，请稍后重试";
                            Toast.makeText(LoginActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
                        }
                    }

                });
            }
        });
    }

    // 保存用户信息到SharedPreferences
    private void saveUserInfo(LoginData loginData) {
        SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        // 保存token
        editor.putString("token", loginData.getToken());

        // 保存用户信息
        if (loginData.getUserInfo() != null) {
            editor.putString("nickname", loginData.getUserInfo().getNickName());
            editor.putString("avatar", loginData.getUserInfo().getAvatarUrl());
        }

        editor.apply();
    }
}